<template>
  <div class="login-lay">
    <div class="login-bg">
      <div class="login-group">
        <div class="login-title">
          <h2>徐海教务系统后台登录</h2>
        </div>
        <div class="login-form">
          <el-form :model="loginForm" :rules="rules" ref="loginFrom" label-width="80px" @keydown.enter.native="login">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" show-password v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
              <el-input v-model="loginForm.code">
                <template slot="append">
                  <img :src="kaptchaSrc" @click="getKaptchaSrc" style="display: block;height: 40px;width: 150px;" alt="验证码加载失败" title="点击切换验证码">
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="login">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { uuid } from 'vue-uuid'

export default {
  name: 'LoginView',
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      kaptchaSrc: ''
    }
  },
  methods: {
    login () {
      this.$refs.loginFrom.validate(valid => {
        if (valid) {
          this.$http.post('admin/login', this.loginForm).then(data => {
            this.$cookies.set('token', data.data)
            this.$message.success('登录成功！')
            this.$router.replace('/admin')
          }).catch(() => {
            this.getKaptchaSrc()
          })
        }
      })
    },
    getKaptchaSrc () {
      this.loginForm.uuid = uuid.v4()
      this.kaptchaSrc = `${process.env.VUE_APP_API}admin/kaptcha.jpg?uuid=${this.loginForm.uuid}`
    }
  },
  created () {
    this.getKaptchaSrc()
  }
}
</script>

<style>
.el-form-item__label{
  color: rgba(0, 0, 0, 0.8);
  font-size: 16px;
}
.el-input-group__append{
  padding: 0;
  border: none;
}
</style>

<style scoped lang="scss">
.login-lay{
  height: 100vh;
  width: 100vw;
  overflow: hidden;/*超出部分隐藏*/
  .login-bg{
    height: 100%;
    width: 100%;
    background: url("../../assets/bg.jpg") no-repeat center/cover;
    display: flex;
    align-items: center;
    justify-content: center;
    .login-group{
      height: 40%;
      width: 25%;
      background-color: rgba(255,255,255,.4);
      border-radius: 15px;
      .login-title{
        height: 20%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .login-form{
        width: 90%;
      }
    }
  }
}
</style>
